﻿using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Threading;
using TheOliver.DigitalSignage.Client.Interfaces;
using System.Collections.Generic;

namespace CeBIT2010
{
    public partial class CebitAgenda : UserControl, IInteractiveSlide
    {
        public CebitAgenda()
        {
            InitializeComponent();
        }

        double _gap;
        double _itemHeight;

        private void ShowItems()
        {
            if (Configuration.AllSlots != null && Configuration.AllSlotsByRoom != null)
            {
                //int i = 0;
                int minGap = Configuration.MinAgendaItemGap;
                DispatcherTimer waitTimer;

                // Get Session for selected Date
                IEnumerable<AdminApp.Web.AllSlots> allSlotsOrdered = Configuration.TodaysUpcomingSessions;

                CebitAgendaItem cai = new CebitAgendaItem();
                _itemHeight = cai.Height;
                int slotNo = allSlotsOrdered.Count();
                double maxHeight = slotNo * (_itemHeight + minGap);
                double pagesNeed = maxHeight / (_itemsPanel.ActualHeight);
                pagesNeed = Math.Ceiling(pagesNeed);

                double heightPerPage = maxHeight / pagesNeed;
                int currentItem = 0;
                int currentPage = 1;
                int itemsPerPage = (int)(allSlotsOrdered.Count() / pagesNeed);
                _gap = (heightPerPage - itemsPerPage * (_itemHeight + minGap)) 
                    / itemsPerPage;

                int itemsOnPage = 0;
                DispatcherTimer itemTimer = new DispatcherTimer();
                itemTimer.Interval = new TimeSpan(0, 0, 0, 0
                    , Configuration.AgendaItemDefaultDisplayTimeInMilliSeconds);
                itemTimer.Tick += (s, e) =>
                    {
                        itemTimer.Stop();

                        if (currentPage <= pagesNeed &&
                            currentItem < allSlotsOrdered.Count())
                        {
                            if (itemsOnPage > itemsPerPage)
                            {
                                // Wait a second, clean page and continue
                                waitTimer = new DispatcherTimer();
                                waitTimer.Interval = new TimeSpan(0, 0, 0, 0
                                    , Configuration.AgendaItemDefaultWaitTimeInMilliseconds);
                                waitTimer.Tick += (s1, e1) =>
                                    {
                                        waitTimer.Stop();
                                        _itemsPanel.Children.Clear();
                                        itemsOnPage = 0;
                                        currentPage++;
                                        itemTimer.Start();
                                    };
                                waitTimer.Start();
                            }
                            else
                            {
                                // Add Item
                                var slot = allSlotsOrdered.ToArray()[currentItem];

                                CebitAgendaItem item = new CebitAgendaItem();
                                item.DataContext = slot;
                                item.SetValue(Canvas.TopProperty, itemsOnPage * (_gap + _itemHeight));
                                _itemsPanel.Children.Add(item);
                                item.Width = _itemsPanel.ActualWidth;

                                currentItem++;
                                itemsOnPage++;
                                itemTimer.Start();
                            }
                        }
                        else
                        {
                            // Last Item displayed
                            waitTimer = new DispatcherTimer();
                            waitTimer.Interval = new TimeSpan(0, 0, 0, 0, Configuration.AgendaItemDefaultWaitTimeInMilliseconds);
                            waitTimer.Tick += (s1, e1) =>
                            {
                                waitTimer.Stop();
                                OnFinished();
                            };
                            waitTimer.Start();
                            return;
                        }
                    };
                itemTimer.Start();
            }
            else
            {
                OnFinished();
            }
        }

        #region ISlide Members

        public event EventHandler Finished;
        private void OnFinished()
        {
            if (Finished != null)
            {
                Finished(this, EventArgs.Empty);
            }
        }

        public int DisplayDuration
        {
            get { return 0; }
        }

        public bool HasContent
        {
            get
            {
                return Configuration.TodaysUpcomingSessions.Count() > 0;
            }
        }


        #endregion

        #region IAgenda Members

        public void Start()
        {
            if (!this._contentLoaded)
            {
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(0, 0, 2);
                timer.Tick += (s, e) =>
                    {
                        timer.Stop();
                        Start();
                    };
                timer.Start();
            }
            else
            {
                ShowItems();
            }

        }

        #endregion

    }
}
